package com.tmtc.serviceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tmtc.constant.ParameterConstant;
import com.tmtc.constant.SystemVar;
import com.tmtc.dao.ArrivalTimeDao;
import com.tmtc.dao.CarDao;
import com.tmtc.dao.CarLocationDao;
import com.tmtc.dao.LineStationDao;
import com.tmtc.dao.LineTimeDao;
import com.tmtc.frame.PageResult;
import com.tmtc.po.ArrivalTime;
import com.tmtc.po.ArrivalTimeRepository;
import com.tmtc.po.Car;
import com.tmtc.po.CarLocation;
import com.tmtc.po.CarLocationRepository;
import com.tmtc.po.CarLocationRepository.Criteria;
import com.tmtc.po.CarRepository;
import com.tmtc.po.LineStation;
import com.tmtc.po.LineStationRepository;
import com.tmtc.po.LineTime;
import com.tmtc.service.CarLocationService;
import com.tmtc.utils.IdWorker;
import com.tmtc.utils.VerificationUtil;

@Service
public class CarLocationServiceImpl implements CarLocationService {

	// public static final double DISTANCE = 0.05;

	@Autowired
	CarLocationDao carLocationDao;

	@Autowired
	CarDao carDao;

	@Autowired
	LineTimeDao lineTimeDao;

	@Autowired
	LineStationDao lineStationDao;

	@Autowired
	ArrivalTimeDao arrivalTimeDao;
	
	@Override
	public int count(CarLocationRepository example) {
		return 0;
	}

	@Override
	public int delete(String id) {
		return carLocationDao.deleteByPrimaryKey(id);
	}

	@Override
	public int delete(CarLocationRepository example) {
		return 0;
	}

	@Override
	public int insert(CarLocation record) {
		return carLocationDao.insert(record);
	}

	@Override
	public List<CarLocation> select(CarLocationRepository example) {
		return carLocationDao.selectByExample(example);
	}

	@Override
	public CarLocation selectByPrimaryKey(String id) {
		return null;
	}

	@Override
	public int update(CarLocation record) {
		return carLocationDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public int update(CarLocation record, CarLocationRepository example) {
		return 0;
	}

	@Override
	public PageResult selectByPage(CarLocationRepository example) {
		return null;
	}

	@Override
	public List<CarLocation> nearBus(Double x, Double y) {
		CarLocationRepository carLocationRepository = new CarLocationRepository();
		// CarLocationRepository.Criteria criteria =
		// carLocationRepository.createCriteria();
		// criteria.andX_pointBetween(x - DISTANCE, x + DISTANCE);
		// criteria.andY_pointBetween(y - DISTANCE, y + DISTANCE);
		List<CarLocation> listCarLocation = select(carLocationRepository);

		List<CarLocation> list = new ArrayList<CarLocation>();
		for (CarLocation cl : listCarLocation) {
			CarRepository carRepository = new CarRepository();
			CarRepository.Criteria criteriaCar = carRepository.createCriteria();
			criteriaCar.andImeiEqualTo(cl.getDevice_id());
			List<Car> listCar = carDao.selectByExample(carRepository);
			if (VerificationUtil.size(listCar) > 0) {
				Car car = listCar.get(0);
				cl.setCar(car);
				list.add(cl);
			}
		}

		// List<DistanceVo> listVo = new ArrayList<DistanceVo>();
		// for (CarLocation carLocation : listCarLocation) {
		// Double carX = carLocation.getX_point();
		// Double carY = carLocation.getY_point();
		// Double xAbs = Math.abs(carX - x);
		// Double yAbs = Math.abs(carY - y);
		// double distance = Math.sqrt(Math.pow(xAbs, 2) * Math.pow(yAbs, 2));
		// DistanceVo distanceVo = new DistanceVo();
		// distanceVo.setDistance(distance);
		// distanceVo.setCarLocation(carLocation);
		// listVo.add(distanceVo);
		// }
		// Collections.sort(listVo, new Comparator<DistanceVo>() {
		// public int compare(DistanceVo distanceVo1, DistanceVo distanceVo2) {
		// if (distanceVo1.getDistance() < distanceVo2.getDistance()) {
		// return -1;
		// } else if (distanceVo1.getDistance() > distanceVo2.getDistance()) {
		// return 1;
		// } else {
		// return 0;
		// }
		// }
		// });
		// if (listVo.size() >= 10) {
		// listVo.subList(10, listVo.size()).clear();
		// }
		// List<CarLocation> list = new ArrayList<CarLocation>();
		// for (DistanceVo distanceVo : listVo) {
		// CarRepository carRepository = new CarRepository();
		// CarRepository.Criteria criteriaCar = carRepository.createCriteria();
		// String IMEI = distanceVo.getCarLocation().getDevice_id();
		// criteriaCar.andImeiEqualTo(IMEI);
		// criteriaCar.andIs_checkEqualTo(ParameterConstant.IS_CHECK);
		// criteriaCar.andCar_statusEqualTo(ParameterConstant.CAR_STATUS_IS);
		// List<Car> listCar = carDao.selectByExample(carRepository);
		// Car car = null;
		// CarLocation carLocation = null;
		// if (VerificationUtil.size(listCar) > 0) {
		// car = listCar.get(0);
		// carLocation = distanceVo.getCarLocation();
		// carLocation.setCar(car);
		// }
		// if(null != carLocation){
		// list.add(carLocation);
		// }
		// }
		return list;
	}

	@Override
	public int updateCarLocation(String device_id, Double x_point, Double y_point, String lineTimeId) {

		try {
			if (0 == VerificationUtil.length(lineTimeId)) {
				return 2;
			}
			LineTime lineTime = lineTimeDao.selectByPrimaryKey(lineTimeId);
			if (null == lineTime || 0 == VerificationUtil.length(lineTime.getLine_id())) {
				return 2;
			}
			LineStationRepository lineStationRepository = new LineStationRepository();
			com.tmtc.po.LineStationRepository.Criteria criteria = lineStationRepository.createCriteria();
			criteria.andLine_idEqualTo(lineTime.getLine_id());
			criteria.andIs_checkEqualTo(ParameterConstant.IS_CHECK);
			List<LineStation> lineStationlist = lineStationDao.selectByExample(lineStationRepository);
			if (0 == VerificationUtil.size(lineStationlist)) {
				return 2;
			}
			for (LineStation ls : lineStationlist) {
				Double x_point2 = ls.getX_point();
				Double y_point2 = ls.getY_point();
				double absx = Math.abs(x_point - x_point2);
				double absy = Math.abs(y_point - y_point2);
				if (SystemVar.DISTANCE > absx && SystemVar.DISTANCE > absy) {
					LineTime lineTime2 = new LineTime();
					lineTime2.setId(lineTimeId);
					lineTime2.setLine_station_sort(ls.getSort());
					lineTimeDao.updateByPrimaryKeySelective(lineTime2);
					// 统计到站时间
					LineStationRepository lineStationRepository2 = new LineStationRepository();
					LineStationRepository.Criteria criteria2 = lineStationRepository.createCriteria();
					criteria2.andLine_idEqualTo(lineTime.getLine_id());
					criteria2.andSortEqualTo(ls.getSort());
					List<LineStation> line_station_list = lineStationDao.selectByExample(lineStationRepository2);
					if (0 != VerificationUtil.size(line_station_list)) {
						String stationId = line_station_list.get(0).getId();
						ArrivalTimeRepository arrivalTimeRepository = new ArrivalTimeRepository();
						com.tmtc.po.ArrivalTimeRepository.Criteria criteria3 = arrivalTimeRepository.createCriteria();
						criteria3.andLine_time_idEqualTo(lineTimeId);
						criteria3.andStation_idEqualTo(stationId);
						List<ArrivalTime> list = arrivalTimeDao.selectByExample(arrivalTimeRepository);
						if (0 == VerificationUtil.size(list)) {
							ArrivalTime arrivalTime = new ArrivalTime(IdWorker.nextId(), lineTimeId, stationId,
									new Date());
							arrivalTimeDao.insert(arrivalTime);
							break;
						}
					} else {
						return 3;
					}
				}
			}
			// 修改班车位置
			CarLocationRepository carLocationRepository = new CarLocationRepository();
			Criteria criteria1 = carLocationRepository.createCriteria();
			criteria1.andDevice_idEqualTo(device_id);
			List<CarLocation> carLocationlist = carLocationDao.selectByExample(carLocationRepository);
			if (0 == VerificationUtil.size(carLocationlist)) {
				CarLocation carLocation = new CarLocation(IdWorker.nextId(), x_point, y_point, device_id, new Date());
				return carLocationDao.insert(carLocation);
			}
			CarLocation carLocation = new CarLocation(carLocationlist.get(0).getId(), x_point, y_point, device_id,
					new Date());
			return carLocationDao.updateByPrimaryKeySelective(carLocation);
		} catch (Exception e) {
			return 2;
		}

	}

	@Override
	public LineStation getNewLineStation(String lineTimeId) {
		LineTime lineTime = lineTimeDao.selectByPrimaryKey(lineTimeId);
		if (null == lineTime) {
			return null;
		}
		LineStationRepository lineStationRepository = new LineStationRepository();
		com.tmtc.po.LineStationRepository.Criteria criteria = lineStationRepository.createCriteria();
		criteria.andLine_idEqualTo(lineTime.getLine_id());
		criteria.andSortEqualTo(lineTime.getLine_station_sort());
		List<LineStation> list = lineStationDao.selectByExample(lineStationRepository);
		if(1 != VerificationUtil.size(list))
			return null;
		LineStation lineStation = list.get(0);
		return lineStation;
	}
}
